/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package administracion.propiedades;
import administracion.helper.Conversiones;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Renato
 */
public class Propiedades {
private static Properties propPrincipal;
private static String ip;
private static String mac;
private static String host;
private static InetAddress direccionLocal;
private static String rutaPropiedades="PropiedadesPrincipales.properties";//"prop\\";

    public static Properties leePropPrincipal() throws java.io.IOException,Exception{
        if(propPrincipal==null)
        {
            propPrincipal=new java.util.Properties();
            java.io.InputStream is = null;

            
            try {
               
                is=new java.io.FileInputStream(rutaPropiedades);
                propPrincipal.load(is);
                escribirHoraAcceso();
                try {
                    estableceDireccion(propPrincipal.getProperty("usuario.host"),
                            Conversiones.convierteHexadecimalString_ArrayByte(propPrincipal.getProperty("usuario.direcc")));
                } catch (UnknownHostException ex) {
                    Logger.getLogger(Propiedades.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SocketException ex) {
                    Logger.getLogger(Propiedades.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(Propiedades.class.getName()).log(Level.SEVERE, null, ex);
                }
                } catch(java.io.IOException ioe) {
                    throw ioe;
                }
            }
        return propPrincipal;
      }

    public static void escribePropiedades() throws Exception {
        java.io.OutputStream os=new java.io.FileOutputStream(rutaPropiedades);
        propPrincipal.store(os, "");
    }

    private static InetAddress getDireccion() throws UnknownHostException, SocketException, Exception{
        if(direccionLocal==null){
            direccionLocal=InetAddress.getLocalHost();
            estableceDireccion(direccionLocal.getHostName(),direccionLocal.getAddress());
        }
        return direccionLocal;
    }
    public static void estableceDireccion(String host1,byte[] direcc) throws UnknownHostException, SocketException, Exception{
        propPrincipal.setProperty("usuario.direcc",Conversiones.convierteArrayByte_HexadecimalString(direcc));
        direccionLocal=InetAddress.getByAddress(host1,direcc);
        escribePropiedades();

        ip=direccionLocal.getHostAddress();
        propPrincipal.setProperty("usuario.ip", ip);
        escribePropiedades();

        host=direccionLocal.getHostName();
        propPrincipal.setProperty("usuario.host", host);
        escribePropiedades();

        NetworkInterface ntwi=NetworkInterface.getByInetAddress(direccionLocal);
        mac=Conversiones.convierteArrayByte_HexadecimalString(ntwi.getHardwareAddress());
        propPrincipal.setProperty("usuario.mac", mac);
        escribePropiedades();
    }

    public static void escribirHoraAcceso() throws Exception{
        String fecha=DateFormat.getInstance().format(new Date());
        propPrincipal.setProperty("usuario.ultimoacceso",fecha);
        escribePropiedades();
    }

    public static Date getHoraAcceso() throws Exception{
         if(propPrincipal==null){
             leePropPrincipal();
         }
         DateFormat algo=DateFormat.getInstance();

        return algo.parse(propPrincipal.getProperty("usuario.ultimoacceso"));
    }
    public static String getIp() throws Exception{
        if(ip==null){
            ip=getDireccion().getHostAddress();
        }
        return ip;
    }

    public static String getHost() throws Exception{
        if(host==null){
            host=getDireccion().getHostName();
        }
        return host;
    }

    public static List obtenerInfoRedes() throws SocketException{
        List lista=new ArrayList(1);
        java.net.NetworkInterface ntwi;

        java.util.Enumeration<java.net.NetworkInterface> enume=java.net.NetworkInterface.getNetworkInterfaces();
        while(enume.hasMoreElements()){
            ntwi=enume.nextElement();
            List<InterfaceAddress> direccList=ntwi.getInterfaceAddresses();
                for(InterfaceAddress direc:direccList){
                    List aux=new ArrayList(1);
                    aux.add(ntwi);
                    aux.add(direc.getAddress());
                    lista.add(aux);
                }
        }

        return lista;
    }

     public static void setUsuario(String usuario) throws Exception {
        if(propPrincipal==null){
             leePropPrincipal();
         }
         propPrincipal.setProperty("servidor.usuario", usuario);
        escribePropiedades();
     }
     public static void setEstado(String estado) throws Exception {
        if(propPrincipal==null){
             leePropPrincipal();
         }
         propPrincipal.setProperty("servidor.estado", estado);
        escribePropiedades();
     }
     public static String getEstado() throws Exception {
        if(propPrincipal==null){
             leePropPrincipal();
         }
        return propPrincipal.getProperty("servidor.estado");

     }
     public static String getUsuario() throws Exception{
         if(propPrincipal==null){
             leePropPrincipal();
         }

         String ret="";
         ret=propPrincipal.getProperty("servidor.usuario");
         return ret;
     }

     public static String getMac() throws Exception{
        if(mac==null){

            java.net.NetworkInterface ntwi=java.net.NetworkInterface.getByInetAddress(getDireccion());
            java.util.Enumeration<java.net.NetworkInterface> enume=java.net.NetworkInterface.getNetworkInterfaces();
            while(enume.hasMoreElements()){
                ntwi=enume.nextElement();              
            java.util.Enumeration<InetAddress> enumen2=ntwi.getInetAddresses();
            while(enumen2.hasMoreElements())                            
            {
                enumen2.nextElement();
                try{
                mac=Conversiones.convierteArrayByte_HexadecimalString(ntwi.getHardwareAddress());                
                }catch(Exception e){
                }
            }
        }
        }
        return mac;
    }

     public static void main(String arg[]){
         try{
             leePropPrincipal();
                System.out.println(Propiedades.getIp());
                System.out.println(Propiedades.getHost());
                System.out.println(Propiedades.getMac());
         }catch(Exception e){
             e.printStackTrace();
         }
     }
}
